package com.itheima.myCollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Demo1 {
    public static void main(String[] args) {
        //多态构建集合
        Collection<String> coll = new ArrayList();
        //添加
        coll.add("张三");
        coll.add("李四");
        coll.add("王五");
        //输出集合长度
        int size = coll.size();
        System.out.println("size = " + size);
        //判断以及删除
        if(coll.contains("张三")){
            coll.remove("张三");
        }
        //再次输出长度
       size = coll.size();
        System.out.println("size = " + size);
        //判断是否为空
        boolean empty = coll.isEmpty();
        System.out.println("empty = " + empty);
        //打印字符串
        String s = coll.toString();
        System.out.println("s = " + s);

        //遍历collection集合,用迭代器思想
        //1.先获取迭代器对象 //Tterator是个接口
        Iterator<String> it = coll.iterator();
        //通过it来完成集合
        while(it.hasNext()){
            String name = it.next();
            System.out.println("name = " + name);
        }
        while(it.hasNext()){
            String name = it.next();
            if("王五".equals(name)){
               it.remove();
            }
            System.out.println("name = " + name);
        }
    }
}
